<template>
  <div>
    <van-row gutter="20" class="title_left">
      <van-col span="8" style="padding-top: 45px">
        <a @click="back" class="back_a" style="color: white; margin-left: 10px">
          返回
        </a>
      </van-col>
      <van-col
        span="8"
        style="text-align: center; color: white; padding-top: 45px"
        >设备解绑</van-col
      >
      <van-col span="8"></van-col>
    </van-row>
    <van-row class="topMargin">
      <van-col span="24">
        <van-cell title="设备绑定详情" class="bg_grey" />
        <!-- <van-form> -->
          <van-field label="车牌号码" :value="vehicle.plateNum" readonly />
          <van-field label="SN码" :value="vehicle.sn" readonly />
          <van-field label="PIN码" :value="vehicle.pin" readonly />
        <!-- </van-form> -->
        <div style="margin: 16px">
          <van-button block type="primary" @click="commit">提交</van-button>
        </div>
      </van-col>
    </van-row>
  </div>
</template>
<script>
import { Api } from "../../../../api/api_config";
import { Toast } from "vant";
import { Maintain, MaintainFinish } from "../../../../model/maintain";

export default {
  data() {
    return {
      vehicle: {},
      currentUser: {},
      uploader: [],
      bindDate: "",
      showBindDatePicker: false,
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.$store.state.tab_show = false;
      this.currentUser = this.$store.state.configuration.currentUser;
      this.vehicle = this.$store.state.vehicle;
      console.log(this.vehicle);
    },
    back() {
      this.$router.push("/carUsage/carManage");
    },
    commit() {
      let that = this;

      this.$axios({
        method: Api.Vehicle.put_vehicle.method,
        url: Api.Vehicle.put_vehicle.url,
        data: this.vehicle,
      })
        .then(function () {
          let tempdata = new Maintain();
          tempdata.contact = that.currentUser.name;
          tempdata.contactPhone = that.currentUser.phoneNumber;
          tempdata.vehicleId = that.vehicle.id;
          tempdata.deviceMaintainType = 3;
          tempdata.type = 3;
          that
            .$axios({
              method: Api.maintain.maintain.method,
              url: Api.maintain.maintain.url,
              data: tempdata,
            })
            .then(function (res) {
              let data = new MaintainFinish();
              data.deviceInfo.identifyingCode = that.vehicle.pin;
              data.deviceInfo.sn = that.vehicle.sn;
              that.uploader.forEach((e) => {
                data.filelist.push({ id: e.id, type: 9 });
              });
              data.id = res.data.id;
              data.IsChangeSim = false;
              data.IsInstallDevice = false;

              that
                .$axios({
                  method: Api.maintain.finish.method,
                  url:
                    Api.maintain.maintain.url + "/" + res.data.id + "/finish",
                  data: data,
                })
                .then(function () {
                  Toast.success("解绑成功");
                  setTimeout(function () {
                    that.back();
                  }, 1000);
                })
                .catch(function () {
                  Toast.success("解绑失败，SN或PIN码错误。");
                  setTimeout(function () {
                    that.back();
                  }, 2000);
                });
            })
            .catch(function (e) {
              console.log(e);
            });
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  },
};
</script>